using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

// khai bao namepace
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace WF1.lab05
{
    public partial class frmRegister : Form
    {
        // khai bao cac bien
        string strCon;
        SqlConnection con;

        string strCom;
        SqlCommand com;

        SqlDataReader reader;

        public frmRegister()
        {
            InitializeComponent();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            //kiem tra du lieu
            Regex email = new Regex(@"^[A-z0-9]+([\.\-_]\w+)*@\w+([\.\-_]\w+)*(\.\w{2,4})+$");

            if (txtUsername.Text == "")
            {
                MessageBox.Show("Username can't be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUsername.Focus();
            }
            else if (txtUsername.Text.Length < 6)
            {
                MessageBox.Show("Username must geater than 6 character.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUsername.Focus();
            }
            else if (txtPass1.Text == "")
            {
                MessageBox.Show("Password can't be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPass1.Focus();
            }
            else if (txtPass1.Text.Length < 6)
            {
                MessageBox.Show(" Password must geater than 6 character.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPass1.Focus();
            }
            else if (txtPass1.Text != txtPass2.Text)
            {
                MessageBox.Show("Password don't match.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPass2.Focus();
            }
            else if (txtFullname.Text == "")
            {
                MessageBox.Show("Fullname can't be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtFullname.Focus();
            }
            else if (dpkBirthday.Value.Date > DateTime.Today.Date)
            {
                MessageBox.Show("Invalid Birthday.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dpkBirthday.Focus();
            }
            else if (email.IsMatch(txtEmail.Text)==false)
            {
                MessageBox.Show("Invalid Email.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail.Focus();
            }
            else
            {
                // kiem tra tai khoan co ton tai hay chua
                strCom = "select count(*) from Account where username = @u";
                // tao doi tuong commant
                com = new SqlCommand(strCom, con);

                // truyen tham so
                com.Parameters.AddWithValue("@u", txtUsername.Text);

                int countUser = Convert.ToInt32(com.ExecuteScalar());

                // kiem tra email  co ton tai hay chua
                strCom = "select count(*) from Account where email = @e";
                // tao doi tuong commant
                com = new SqlCommand(strCom, con);

                // truyen tham so
                com.Parameters.AddWithValue("@e", txtUsername.Text);

                int countEmail = Convert.ToInt32(com.ExecuteScalar());

                if (countUser > 0)
                {
                    MessageBox.Show("Please enter another username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (countEmail > 0)
                {
                    MessageBox.Show("Please enter another email address.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    // cau lenh insert
                    strCom = "insert into Account values(@u,@p,@f,@g,@b,@e)";
                    // tao doi tuong commant
                    com = new SqlCommand(strCom, con);
                    // truyen tham so
                    com.Parameters.AddWithValue("@u", txtUsername.Text);
                    com.Parameters.AddWithValue("@p", txtPass1.Text);
                    com.Parameters.AddWithValue("@f", txtFullname.Text);
                    com.Parameters.AddWithValue("@g", rdoMale.Checked);
                    com.Parameters.AddWithValue("@b", dpkBirthday.Value);
                    com.Parameters.AddWithValue("@e", txtEmail.Text);

                    try
                    {
                        com.ExecuteNonQuery();
                        MessageBox.Show("Register is successful.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    
                }
            }
        }

        private void frmRegister_Load(object sender, EventArgs e)
        {
            try
            {
                //1. tao chuoi ket noi
            strCon = "server=LAB04_06;database=Northwind;uid=sa;pwd=sa";
            
            //2. tao doi tuong connection
            con = new SqlConnection(strCon);

            //3. mo ket noi
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not connect to database.\nError:\n"+ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                Application.Exit();                
            }
        }

        
    }
}